#!/usr/bin/env python

# --------------------------------------
# projects/python/primes/TestPrimes.py
# Copyright (C) 2009
# Glenn P. Downing
# --------------------------------------

# To run the tests
#     TestCollatz.py

# To document the tests
#     pydoc -w TestCollatz

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestPrimes
# -----------
class TestPrimes (unittest.TestCase) :
    """
    Tests the read, write, isPrime, and Eval methods of the main.py program.
    """
    
    
    # ----
    # read
    # ----

    def test_read (self) :
        reader = TestReader('104')
        main.my_read(reader)
        self.assert_(main.data ==  104)

    # ----
    # eval
    # ----

    def test_isPrime1 (self) :
	main.primeCache()
	self.assert_(main.isPrime(13))
		
    def test_isPrime2 (self) :
	self.assert_(main.isPrime(997))
		
    def test_isPrime3 (self) :
	self.assert_(main.isPrime(853))
		
    def test_isPrime4 (self) :
	self.assert_(not main.isPrime(3100))
		
    def test_isPrime5 (self) :
	self.assert_(not main.isPrime(3162))
		
    def test_isPrime6 (self) :
	self.assert_(main.isPrime(999979))
	
    def test_eval1 (self) : 
	main.answer1 = 0
	main.answer2 = 0
	main.answer3 = 0
	main.answer4 = 0
	main.data = 12
	main.my_eval()
	self.assert_(main.isPrime(main.answer1))
	self.assert_(main.isPrime(main.answer2))
	self.assert_(main.isPrime(main.answer3))
	self.assert_(main.isPrime(main.answer4))
	
    def test_eval2 (self) : 
	main.answer1 = 0
	main.answer2 = 0
	main.answer3 = 0
	main.answer4 = 0
	main.data = 47
	main.my_eval()
	self.assert_(main.isPrime(main.answer1))
	self.assert_(main.isPrime(main.answer2))
	self.assert_(main.isPrime(main.answer3))
	self.assert_(main.isPrime(main.answer4))
	
    def test_eval3 (self) : 
	main.answer1 = 0
	main.answer2 = 0
	main.answer3 = 0
	main.answer4 = 0
	main.data = 66423
	main.my_eval()
	self.assert_(main.isPrime(main.answer1))
	self.assert_(main.isPrime(main.answer2))
	self.assert_(main.isPrime(main.answer3))
	self.assert_(main.isPrime(main.answer4))
	
    def test_eval4 (self) : 
	main.answer1 = 0
	main.answer2 = 0
	main.answer3 = 0
	main.answer4 = 0
	main.data = 57235
	main.my_eval()
	self.assert_(main.isPrime(main.answer1))
	self.assert_(main.isPrime(main.answer2))
	self.assert_(main.isPrime(main.answer3))
	self.assert_(main.isPrime(main.answer4))
	
    def test_eval5 (self) : 
	main.answer1 = 0
	main.answer2 = 0
	main.answer3 = 0
	main.answer4 = 0
	main.data = 7
	main.my_eval()
	self.assert_(not main.possible)
	

    # -----
    # print
    # -----

    def test_print (self) :
        writer = TestWriter()
        main.answer1 = 1
        main.answer2 = 2
        main.answer3 = 3
        main.answer4 = 4
        main.possible = True
        main.my_print(writer)
        self.assert_(writer.str() == '1 2 3 4\n')
	
	def test_print2 (self) :
		writer = TestWriter()
		main.possible = False
		main.my_print(writer)
		self.assert_(writer.str() == "Impossible.")


if __name__ == "__main__" :
    unittest.main()
